//
//  UIButton+touch.m
//  Runone
//  @By akkeLyn
//  Created by runone02 on 17/2/13.
//  Copyright © 2017年 runone. All rights reserved.
//  用于防止按钮重复点击

#import "UIButton+touch.h"
#import <objc/runtime.h>

@interface UIButton ()

@property (nonatomic, assign) NSTimeInterval custom_acceptEventTime; //记录上次的点击的时间

@end

@implementation UIButton (touch)

//+ (void)load{
//    Method systemMethod = class_getInstanceMethod(self, @selector(sendAction:to:forEvent:));
//    SEL sysSEL = @selector(sendAction:to:forEvent:);
//    
//    Method customMethod = class_getInstanceMethod(self, @selector(custom_sendAction:to:forEvent:));
//    SEL customSEL = @selector(custom_sendAction:to:forEvent:);
//    
//    BOOL didAddMethod = class_addMethod(self, sysSEL, method_getImplementation(customMethod), method_getTypeEncoding(customMethod));
//
//    if (didAddMethod) {
//        class_replaceMethod(self, customSEL, method_getImplementation(systemMethod), method_getTypeEncoding(systemMethod));
//    }else{
//        method_exchangeImplementations(systemMethod, customMethod);
//    }
//}
//
//- (NSTimeInterval )custom_acceptEventInterval {
//    return [objc_getAssociatedObject(self, "UIControl_acceptEventInterval") doubleValue];
//}
//
//- (void)setCustom_acceptEventInterval:(NSTimeInterval)custom_acceptEventInterval{
//    objc_setAssociatedObject(self, "UIControl_acceptEventInterval", @(custom_acceptEventInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
//}
//
//- (NSTimeInterval )custom_acceptEventTime{
//    return [objc_getAssociatedObject(self, "UIControl_acceptEventTime") doubleValue];
//}
//
//- (void)setCustom_acceptEventTime:(NSTimeInterval)custom_acceptEventTime{
//    objc_setAssociatedObject(self, "UIControl_acceptEventTime", @(custom_acceptEventTime), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
//}
//
//- (void)custom_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event{
//    // 是否小于设定的时间间隔
//    self.custom_acceptEventInterval = self.custom_acceptEventInterval == 0 ? defaultInterval : self.custom_acceptEventInterval;
//    NSLog(@"%f %f",self.custom_acceptEventTime, self.custom_acceptEventInterval);
//    BOOL needSendAction = (NSDate.date.timeIntervalSince1970 - self.custom_acceptEventTime >= self.custom_acceptEventInterval);
//    
//    // 更新上一次点击时间戳
//    if (self.custom_acceptEventInterval > 0) {
//        self.custom_acceptEventTime = NSDate.date.timeIntervalSince1970;
//    }
//    
//    // 两次点击的时间间隔小于设定的时间间隔时，才执行响应事件
//    if (needSendAction) {
//        [self custom_sendAction:action to:target forEvent:event];
//    }
//}


@end
